import AMapLoader from '@amap/amap-jsapi-loader';

class kkAmap {
  constructor() {
    this.layers = {};
  }

  init(container, cfg, callback) {
    delete cfg['serverKey'];
    AMapLoader.load(cfg.base).then((AMap) => {
      window.map = new AMap.Map(container, {
        ...cfg.default,
      });

      //增加图层 Traffic:实时交通 RoadNet:路网 Satellite:卫星图
      if (cfg.layers && Object.prototype.toString.call(cfg.layers) === '[object Object]') {
        Object.keys(cfg.layers).forEach(name => {
          this.layerHandle().add(name, cfg.layers[name]);
        })
      }
      if (typeof callback === 'function') callback();
    }).catch(e => {
      throw e;
    })
  }

  // 图层事件
  layerHandle = () => {
    return {
      add: (name, cfg) => {
        if (this.layers[name]) return false;
        if (name === 'Default') {

        } else {
          this.layers[name] = new AMap.TileLayer[name](cfg);
          this.layers[name].setMap(window.map);
        }
      },
      show: (name) => {
        if (!this.layers[name]) return false;
        this.layers[name].show();
      },
      hide: (name) => {
        if (!this.layers[name]) return false;
        this.layers[name].hide();
      }
    }
  };

  //标记事件
  markerHandle = () => {
    return {
      add: (cfgArr) => {
        if (!cfgArr instanceof Array) return [];
        const markerList = [];
        cfgArr.forEach(item => {
          markerList.push(new AMap.Marker({
            position: item.position, //new AMap.LngLat(116.39, 39.9)
            title: item.title || ''
          }))
        });
        window.map.add(markerList);
        return markerList;
      },
      del: (markerList) => {
        if (!markerList instanceof Array || markerList.length === 0) return false;
        window.map.remove(markerList);
      }
    }
  };

  // 绘制边界
  getDistrict(name) {
    var district = new AMap.DistrictSearch({
      // 返回行政区边界坐标等具体信息
      extensions: 'all',
      // 设置查询行政区级别为 区
      level: 'district'
    });

    district.search(name, (status, result) => {
      // 获取朝阳区的边界信息
      var bounds = result.districtList[0].boundaries;
      var polygons = [];
      if (bounds) {
        for (var i = 0, l = bounds.length; i < l; i++) {
          //生成行政区划polygon
          var polygon = new AMap.Polygon({
            map: window.map,
            strokeWeight: 1,
            path: bounds[i],
            fillOpacity: .05,
            fillColor: '#CCF3FF',
            strokeColor: 'rgba(255, 166, 77, .5)'
          });
          polygons.push(polygon);
        }
        // 地图自适应
        // window.map.setFitView();
      }
    })
  };

  drawMarker(cfg) {
    cfg = cfg || {};
    const pos = cfg['pos'];
    return new AMap.Marker({
      map: window.map,
      // 第一个点为起点
      position: new AMap.LngLat(pos[0] * 1, pos[1] * 1),
      offset: new AMap.Pixel(-10, -20),
      icon: new AMap.Icon({
        // 图标尺寸
        size: new AMap.Size(26, 52),
        // 图标的取图地址
        image: cfg.icon,
        // 图标所用图片大小
        imageSize: new AMap.Size(20, 40)
      }),
      autoRotation: true
    });
  };

  moveMarker(cfg) {
    try {
      cfg.marker.moveAlong(cfg.passPos, {
        duration: 500,
        autoRotation: true,
      });

      cfg.marker.on('moving',  (e)=> {
        cfg.line.setPath(cfg.allPos.concat(e.passedPath[1]));
        // window.map.setCenter(e.target.getPosition(),true);
      })
    } catch (e) {
      throw e;
    }

  };

  drawLine(cfg) {
    cfg = cfg || {};
    return new window.AMap.Polyline({
      map: window.map,
      showDir: true,
      strokeColor: cfg.color || "#00ff80", //线颜色
      strokeWeight: cfg.width || 8, //线宽
      strokeStyle: cfg.style || "solid",  //线样式
      lineJoin: 'round',
      dirColor:'#ccc'
    });
  };


}

export default new kkAmap();
